package com.jy.adscm.operation.mscyb.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.operation.mscyb.bean.JqcsSetting;
import com.jy.adscm.operation.mscyb.bean.QueryJqbzSettings;
import com.jy.adscm.operation.mscyb.mapper.mysql.JqcsSettingMapper;
import com.jy.adscm.operation.mscyb.service.JqcsSettingService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import com.jy.adscm.zltx.bean.JyDrawingType;
import com.jy.adscm.zltx.mapper.mysql.JyDrawingTypeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2019-05-24
 */
@Service
public class JqcsSettingServiceImpl extends ServiceImpl<JqcsSettingMapper, JqcsSetting> implements JqcsSettingService {
    @Autowired
    JyUserMapper jyUserMapper;
    @Autowired
    JyDrawingTypeMapper jyDrawingTypeMapper;
    @Override
    public JqcsSetting selectJqcsSettingById(String id) {
        JqcsSetting jqcsSetting = super.selectById(id);
        if (AdscmBaseUtils.getInstance().isNotEmpty(jqcsSetting,null)){
            if (AdscmBaseUtils.getInstance().isNotEmpty(jqcsSetting.getCreator(),null)){
                JyUser jyUser = jyUserMapper.selectById(jqcsSetting.getCreator());
                jqcsSetting.setCreatorName(jyUser.getName());
            }

        }
        return jqcsSetting;
    }

    @Override
    public Page<JqcsSetting> selectAll(Page<JqcsSetting> page, QueryJqbzSettings queryJqbzSettings) {
        Wrapper<JqcsSetting> wrapper =new EntityWrapper<>();
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryJqbzSettings.getName(),null)){
            wrapper.eq("name",queryJqbzSettings.getName());
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryJqbzSettings.getCode(),null)){
            wrapper.eq("code",queryJqbzSettings.getCode());
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryJqbzSettings.getType(),null)){
            wrapper.eq("type",queryJqbzSettings.getType());
        }
         wrapper.orderBy("rank",true);
        page= super.selectPage(page,wrapper);
        List<JqcsSetting> jqcsSettings = page.getRecords();
        for (JqcsSetting jqcsSetting : jqcsSettings) {
            if (AdscmBaseUtils.getInstance().isNotEmpty(jqcsSetting.getCreator(),null)){
                JyUser jyUser = jyUserMapper.selectById(jqcsSetting.getCreator());
                jqcsSetting.setCreatorName(jyUser.getName());
            }

            if (AdscmBaseUtils.getInstance().isNotEmpty(jqcsSetting.getType(),null)){
                JyDrawingType jyDrawingType = jyDrawingTypeMapper.selectById(jqcsSetting.getType());
                jqcsSetting.setTypeName(jyDrawingType.getType());
            }
        }

        return page;
    }
}
